﻿// -----------------------------------------------------------------------
// <copyright file="ProfileTypeCompositionProfileListData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;

    /// <summary>
    /// Class access to db ProfileTypeCompositionProfileList
    /// </summary>
    public class ProfileTypeCompositionProfileListData
    {
        /// <summary>
        /// Get ProfileTypeCompositionProfileList By Id
        /// </summary>
        /// <param name="profileTypeCompositionProfileListId">Param profileTypeCompositionProfileListId</param>
        /// <returns>Return ProfileTypeCompositionProfileList</returns>
        public ProfileTypeCompositionProfileListEN GetProfileTypeCompositionProfileListById(int profileTypeCompositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileTypeCompositionProfileListEN profileTypeCompositionProfileList = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListGetById, profileTypeCompositionProfileListId);
                while (reader.Read())
                {
                    profileTypeCompositionProfileList = new ProfileTypeCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeCompositionProfileList.ProfileTypeCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeCompositionProfileList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileTypeCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileTypeCompositionProfileList
        /// </summary>
        /// <returns>Return list all ProfileTypeCompositionProfileList</returns>
        public IList<ProfileTypeCompositionProfileListEN> GetProfileTypeCompositionProfileLists()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeCompositionProfileListEN> lstProfileTypeCompositionProfileList = new List<ProfileTypeCompositionProfileListEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListGetAll);
                while (reader.Read())
                {
                    ProfileTypeCompositionProfileListEN profileTypeCompositionProfileList = new ProfileTypeCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeCompositionProfileList.ProfileTypeCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeCompositionProfileList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }

                    lstProfileTypeCompositionProfileList.Add(profileTypeCompositionProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileTypeCompositionProfileList
        /// </summary>
        /// <param name="profileTypeCompositionProfileList">Paarm ProfileTypeCompositionProfileList</param>
        /// <returns>Insert ProfileTypeCompositionProfileList</returns>
        public int Insert(ProfileTypeCompositionProfileListEN profileTypeCompositionProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.ProfileTypeId, profileTypeCompositionProfileList.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.CompositionProfileListId, profileTypeCompositionProfileList.CompositionProfileListId)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileTypeCompositionProfileList
        /// </summary>
        /// <param name="profileTypeCompositionProfileList">Param ProfileTypeCompositionProfileList</param>
        public void Update(ProfileTypeCompositionProfileListEN profileTypeCompositionProfileList)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.ProfileTypeCompositionProfileListId, profileTypeCompositionProfileList.ProfileTypeCompositionProfileListId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.ProfileTypeId, profileTypeCompositionProfileList.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.CompositionProfileListId, profileTypeCompositionProfileList.CompositionProfileListId)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileTypeCompositionProfileList by profileTypeCompositionProfileListId
        /// </summary>
        /// <param name="profileTypeCompositionProfileListId">param profileTypeCompositionProfileListId</param>
        public void Delete(int profileTypeCompositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeCompositionProfileList.ProfileTypeCompositionProfileListId, profileTypeCompositionProfileListId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileTypeCompositionProfileList By CompositionProfileListId
        /// </summary>
        /// <param name="compositionProfileListId">Param compositionProfileListId</param>
        /// <returns>Return List ProfileTypeCompositionProfileList</returns>
        public IList<ProfileTypeCompositionProfileListEN> GetProfileTypeCompositionProfileListByCompositionProfileListId(int compositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeCompositionProfileListEN> lstProfileTypeCompositionProfileList = new List<ProfileTypeCompositionProfileListEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeCompositionProfileListGetByCompositionProfileListId, compositionProfileListId);
                while (reader.Read())
                {
                    ProfileTypeCompositionProfileListEN profileTypeCompositionProfileList = new ProfileTypeCompositionProfileListEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeCompositionProfileList.ProfileTypeCompositionProfileListId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeCompositionProfileList.ProfileTypeId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeCompositionProfileList.CompositionProfileListId = reader.GetInt32(2);
                    }

                    lstProfileTypeCompositionProfileList.Add(profileTypeCompositionProfileList);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeCompositionProfileList;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Check is used ProfileTypeCompositionProfileList in ProfileType
        /// </summary>
        /// <param name="profileTypeId">param profileTypeId</param>
        /// <param name="profileTypeCompositionProfileListId">param profileTypeCompositionProfileListId</param>
        public bool IsUsed(int profileTypeId, int profileTypeCompositionProfileListId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result =
                    Convert.ToInt32(
                        SqlHelper.ExecuteScalar(
                            SqlHelper.ConnectionString,
                            Constants.StorepProcedure.VOneProfileTypeCompositionProfileListIsUsed,
                            profileTypeId,
                            profileTypeCompositionProfileListId));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}